﻿namespace NiuX.AspNetCore.Responses;

/// <summary>
/// 服务层响应码枚举
/// </summary>
public enum ServiceResultCode
{
    /// <summary>
    /// 成功
    /// </summary>
    Succeed = 0,

    /// <summary>
    /// 失败
    /// </summary>
    Failed = 1,
}

/// <summary>
/// 服务层响应实体
/// </summary>
public class ServiceResult
{
    /// <summary>
    /// 响应码
    /// </summary>
    public ServiceResultCode Code { get; set; }

    /// <summary>
    /// 响应信息
    /// </summary>
    public string Message { get; set; }

    /// <summary>
    /// 成功
    /// </summary>
    public bool Success => Code == ServiceResultCode.Succeed;

    /// <summary>
    /// 时间戳(毫秒)
    /// </summary>
    public long Timestamp { get; } = (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000;

    /// <summary>
    /// 响应成功
    /// </summary>
    /// <param name="message"></param>
    /// <param name="data"></param>
    /// <returns></returns>
    public void IsSuccess(string message = "")
    {
        Message = message;
        Code = ServiceResultCode.Succeed;
    }

    /// <summary>
    /// 响应失败
    /// </summary>
    /// <param name="message"></param>
    /// <param name="data"></param>
    /// <returns></returns>
    public void IsFailed(string message = "")
    {
        Message = message;
        Code = ServiceResultCode.Failed;
    }

    /// <summary>
    /// 响应失败
    /// </summary>
    /// <param name="exexception></param>
    /// <param name="data"></param>
    /// <returns></returns>
    public void IsFailed(Exception exception)
    {
        Message = exception.InnerException?.StackTrace;
        Code = ServiceResultCode.Failed;
    }
}

/// <summary>
/// 服务层响应实体(泛型)
/// </summary>
/// <typeparam name="T"></typeparam>
public class ServiceResult<T> : ServiceResult where T : class
{
    /// <summary>
    /// 返回结果
    /// </summary>
    public T Result { get; set; }

    /// <summary>
    /// 响应成功
    /// </summary>
    /// <param name="result"></param>
    /// <param name="message"></param>
    public void IsSuccess(T result = null, string message = "")
    {
        Message = message;
        Code = ServiceResultCode.Succeed;
        Result = result;
    }
}

public interface IHasTotalCount
{
    /// <summary>
    /// 总数
    /// </summary>
    int Total { get; set; }
}

public interface IListResult<T>
{
    /// <summary>
    /// 返回结果
    /// </summary>
    IReadOnlyList<T> Item { get; set; }
}

public class ListResult<T> : IListResult<T>
{
    IReadOnlyList<T> item;

    public IReadOnlyList<T> Item
    {
        get => item ?? (item = new List<T>());
        set => item = value;
    }

    public ListResult()
    {
    }

    public ListResult(IReadOnlyList<T> item)
    {
        Item = item;
    }
}

public interface IPagedList<T> : IListResult<T>, IHasTotalCount
{
}

/// <summary>
/// 分页响应实体
/// </summary>
/// <typeparam name="T"></typeparam>
public class PagedList<T> : ListResult<T>, IPagedList<T>
{
    /// <summary>
    /// 总数
    /// </summary>
    public int Total { get; set; }

    public PagedList()
    {
    }

    public PagedList(int total, IReadOnlyList<T> result) : base(result)
    {
        Total = total;
    }
}